package cn.xiaoliu.basic.utils;


import cn.xiaoliu.user.domain.Logininfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)){
            //获取spring容器
            WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
            RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate");
            Object logininfo = redisTemplate.opsForValue().get(token);
            if (logininfo !=null){
                return (Logininfo) logininfo;
            }
        }

        return null;
    }
}
